﻿//*** Guidance generated code ***//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;

using ProjectBase.Core;
using ProjectBase.Core.Web;

namespace ProjectBase.Mvc
{
    public class VisualControlCollection : IList<IVisualControl>
    {
        readonly IList<IVisualControl> _controlCollection;

        public VisualControlCollection()
        {
            _controlCollection = new List<IVisualControl>();
        }

        #region ICollection<IVisualControl> Members
        public void Add(IVisualControl item)
        {
            if (item.IsNull())
            {
                return;
            }

            _controlCollection.Add(item);
        }

        public void Clear()
        {
            _controlCollection.Clear();
        }

        public bool Contains(IVisualControl item)
        {
            return _controlCollection.Contains(item);
        }

        public void CopyTo(IVisualControl[] array, int arrayIndex)
        {
            _controlCollection.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _controlCollection.Count; }
        }

        public bool IsReadOnly
        {
            get { return _controlCollection.IsReadOnly; }
        }

        public bool Remove(IVisualControl item)
        {
            var contains = _controlCollection.Contains(item);
            if (contains)
            {
                _controlCollection.Remove(item);
            }
            return contains;
        }

        public IEnumerator<IVisualControl> GetEnumerator()
        {
            return _controlCollection.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _controlCollection.GetEnumerator();
        }
        #endregion

        #region like IList<IVisualControl> Members

        public int IndexOf(IVisualControl item)
        {
            return _controlCollection.IndexOf(item);
        }

        public void AddAt(int index, IVisualControl item)
        {
            _controlCollection.Insert(index, item);
        }
        public void Insert(int index, IVisualControl item)
        {
            AddAt(index, item);
        }

        public void RemoveAt(int index)
        {
            _controlCollection.RemoveAt(index);
        }

        public IVisualControl this[int index]
        {
            get { return _controlCollection[index]; }
            set { _controlCollection[index] = value; }
        }
        #endregion

        public IEnumerable<IVisualControl> FindControls(Func<IVisualControl, bool> predicate)
        {
            var result = _controlCollection.Where(predicate).ToList();
            foreach (var visualControl in _controlCollection)
            {
                result.AddRange(visualControl.Controls.FindControls(predicate));
            }
            return result;
        }
    }
}